/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package phd.dupenois.es.nplusn;

/**
 *
 * @param <T>
 * @author mpd209
 */
public class NplusNEvent<T extends Solution> {
    private Object[] parameters;
    private NplusNEventType type;
    public NplusNEvent(NplusNEventType type, Object[] parameters){
        this.parameters = parameters;
        this.type = type;
    }

    @SuppressWarnings({"unchecked"})
    public void notify(NplusNListener<T> listener){
        if(type.equals(NplusNEventType.SOLUTION_ADDED)){
            listener.onSolutionAdded((T)parameters[0], (Integer)parameters[1]);
        }else if(type.equals(NplusNEventType.SOLUTION_REMOVED)){
            listener.onSolutionRemoved((T)parameters[0], (Integer)parameters[1]);
        }else if(type.equals(NplusNEventType.MUTATION_GENERATED)){
            listener.onMutantGenerated((T)parameters[0], (Integer)parameters[1]);
        }else if(type.equals(NplusNEventType.GENERATION_STARTED)){
            listener.onGenerationStart((Integer)parameters[0]);
        }else if(type.equals(NplusNEventType.GENERATION_FINISHED)){
            listener.onGenerationEnd((Integer)parameters[0]);
        }else if(type.equals(NplusNEventType.START)){
            listener.onStart();
        }else if(type.equals(NplusNEventType.FINISH)){
            Solution[] solutions = (Solution[])parameters[0];
            if(solutions.length>0){
                try {
                    T[] cast = (T[])solutions[0].castArray(solutions);
                    listener.onFinish(cast);
                } catch (ClassCastException e) {
                    throw new IllegalArgumentException("Solution types do not match.");
                }
            }else{
                listener.onFinish();
            }
        }
        
    }
}
